package com.example.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.dto.result;
import com.example.demo.pojo.WiringInfo;
import com.example.demo.service.WiringInfoService;
import com.example.demo.utils.WiringInfoListener;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.excel.EasyExcel;

import java.util.List;

@RestController
@RequestMapping("/api/wiringInfo")
public class WiringInfoController {

    @Autowired
    private final WiringInfoService wiringInfoService;

    public WiringInfoController(WiringInfoService wiringInfoService) {
        this.wiringInfoService = wiringInfoService;
    }

    //批量上传excel文件并将数据插入到数据库中(未测试)
    @PostMapping("/uploadBatch")
    public result uploadExcel(@RequestParam("files") MultipartFile[] files) {
        int count = 1;
        int num = 0;
        for (MultipartFile file : files) {
            System.out.println("第" + count +"张表");
            try {
                // 使用 EasyExcel 解析文件
                List<WiringInfo> wiringInfoList = EasyExcel.read(
                        file.getInputStream(),
                        WiringInfo.class,
                        new WiringInfoListener(wiringInfoService)
                ).sheet().doReadSync();
                //插入数据
                wiringInfoService.saveWiringInfo(wiringInfoList);
                num+=wiringInfoList.size();
            } catch (Exception e) {
                e.printStackTrace();
//                return "上传失败：" + e.getMessage();
                return result.error(e.getMessage());
            }
            count++;
        }
//        return "上传并解析成功，共 " + num + " 条记录";
        return result.success("上传并解析成功，共 " + num + " 条记录");
    }

    //根据所属变电站查找
    @PostMapping("/findBySubstation")
    public result findBySubstation(@RequestParam("substation") String substation,@RequestParam Integer page, @RequestParam Integer size) {
        Page<WiringInfo> SubstationPage = new Page<>(page,size);
        IPage<WiringInfo> res = wiringInfoService.findBySubstation(substation,SubstationPage);
        return result.success(res);
    }

    //根据维护班组查找
    @PostMapping("/findByMaintainTeam")
    public result findByMaintainTeam(@RequestParam("team") String team,@RequestParam Integer page, @RequestParam Integer size) {
        Page<WiringInfo> MaintainTeamPage = new Page<>(page,size);
        IPage<WiringInfo> res = wiringInfoService.findByMaintainTeam(team,MaintainTeamPage);
        return result.success(res);
    }

    //根据所属配变台区查找
    @PostMapping("/findByCourt")
    public result findByCourt(@RequestParam("court") String court,@RequestParam Integer page, @RequestParam Integer size) {
        Page<WiringInfo> CourtPage = new Page<>(page,size);
        IPage<WiringInfo> res = wiringInfoService.findByCourt(court,CourtPage);
        return result.success(res);
    }

    //根据是否光伏用户查找
    @PostMapping("/findByIsPhotovoltaicUser")
    public result findByIsPhotovoltaicUser(@RequestParam("isPhotovoltaicUser") String isPhotovoltaicUser,@RequestParam Integer page, @RequestParam Integer size) {
        Page<WiringInfo> IsPhotovoltaicUserPage = new Page<>(page,size);
        IPage<WiringInfo> res = wiringInfoService.findByIsPhotovoltaicUser(isPhotovoltaicUser,IsPhotovoltaicUserPage);
        return result.success(res);
    }

    //分页获取全部信息
    @GetMapping("/getAll")
    public result<IPage<WiringInfo>> getAllWiringInfo(@RequestParam Integer page, @RequestParam Integer size) {
        Page<WiringInfo> WiringInfoPage = new Page<>(page,size);
        IPage<WiringInfo> res = wiringInfoService.getAllWiringInfo(WiringInfoPage);
        return result.success(res);
    }
}
